function dataOnClick(view) {
  window.open(Urls.resolve('storage:show_data', {did: $(view).attr('data-id')}));
}


//导出数据用到
function export_data(did, type) {
  $.get(Urls.resolve('storage_api_v1:metadata_download'), {
    type: type,
    oid: did
  }).done(function (resp) {
    var fmts = gettext('Data successfully exported! Click %s here %s or `Download` button to download it.');
    $.showModal({
      title: gettext('Success'),
      message: interpolate(fmts, ['<a target="_blank" href="' + resp.data.file + '"><b>', '</b></a>']),
      onOk: function () {
        window.open(resp.data.file);
      },
      okText: gettext('Download')
    });
  }).fail(function (resp) {
    if (resp.status === 401) {
      var next = window.location.href.replace(window.location.origin, '');
      window.location.href = Urls.resolve('account:login') + '?next=' + next;
    } else {
      $.showModal({
        message: resp.responseJSON.msg
      });
    }
  });
}

//数据调用函数
function acquire_result(callback, query_type = 1, time_sort, page, author, year, categroy) {
  // var alg = $('input:radio[name="ra"]:checked').val();
  // author = $('[name="author_name"]').val();
  // alert(author);
  author = author || null;
  year = year || null;
  categroy = categroy || null;
  if (text) {
    if (type_string === 'full') {
      $.requestJSON({
        url: Urls.resolve('search_api_v1:query'),
        method: 'POST',
        data: {
          sort: null,
          desc: time_sort,
          page: page,
          year_filter: year,
          domain_filter: categroy,
          author_filter: author,
          query_dict: {
            text: text
          }
        },
        silentError: true,
        onSuccess: function (data) {
          callback(data);
        },
        onError: function (data) {
          $.showModal({
            title: gettext('ERROR') + ' ' + data.code,
            message: gettext('An error has occurred! Please check your data and try again.')
          });
        }
      });
    } else {
      $.requestJSON({
        url: Urls.resolve('search_api_v1:query'),
        method: 'POST',
        data: {
          sort: null,
          desc: time_sort,
          page: page,
          year_filter: year,
          domain_filter: categroy,
          author_filter: author,
          query_dict: {
            meta: type_string,
            text: text
          }
        },
        silentError: true,
        onSuccess: function (data) {
          callback(data);
        },
        onError: function (data) {
          $.showModal({
            title: gettext('ERROR') + ' ' + data.code,
            message: gettext('An error has occurred! Please check your data and try again.')
          });
        }
      });
    }
  }
}

function result_count(data) {
  $("#result-count").text(data.data.total_count);
}


function download_file(type, view) {
  var id = $(view).parent().parent().parent().parent().attr("data-id");
  export_data(id, type);
}

//去掉的批量下载的布局，如果后需要可以复制过去
// '          <button class="btn btn-default btn-sm btn-add-to-cart" data-tid="' + this['tid'] + '" data-id="' + this['id'] + '"' +
// '                  style="float:right"' +
// '                  data-title="' + this['title'] + '" data-toggle="tooltip" data-placement="left"' +
// '                  title="' + gettext('Add to download list') + '">' +
// '            <i class="fa fa-plus"></i>' +
// '          </button>' +


function search_set_table_hasValue(data) {
  $('#dm-scope').empty();
  var currentPage = data.data.page_num;
  var numberOfPages = data.data.page_count;
  var step = (currentPage - 1) * numberOfPages;

  $.each(data.data.result, function () {
    step++;
    var obj =
      '<div class="data-meta panel" data-id="' + this['id'] + '">' +
      '        <div class="panel-heading">' +
      '          <span class="d-title"   data-id="' + this['id'] + '" onclick="dataOnClick(this)">' + this['title'] + '</span>' +
      '              <span class="d-labels">' +
      '                  <span class="label label-primary">' +
      '                    <i class="fa fa-tag"></i>' + this['keywords'] + '' +
      '                  </span>' +
      '              </span>' +
      '         <div id="download">' +
      '          <button class="btn btn-default btn-sm dropdown-toggle" id="download_single" type="button" data-toggle="dropdown" style="float: right">' +
      '             <i class="fa fa-download"></i>' +
      '             <span class="caret"></span>' +
      '         </button>' +
      '         <ul class="dropdown-menu dropdown-menu-right" role="menu">' +
      '             <li onclick="download_file(\'JSON\',this)"><a>JSON</a></li>' +
      '             <li onclick="download_file(\'XML\',this)"><a>XML</a></li>' +
      '             <li onclick="download_file(\'XLSX\',this)"><a>EXCEL</a></li>' +
      '         </ul>' +
      '         </div>' +
      '        </div>' +
      '        <div class="panel-body">' +
      '          <p>' +
      '            <span class="dm dt">' + gettext('Author') + '</span>' +
      '            <a href="#" class="dm dd author">' + this['author'] + '</a>' +
      '            <span class="dm dt">' + gettext('Add Time') + '</span>' +
      '            <span class="dm dd date">' + this['add_time'] + '</span>' +
      '          </p>' +
      '          <div class="row">' +
      '            <div class="col-lg-4 col-md-4 col-sm-4">' +
      '              <ul class="list-unstyled">' +
      '                <li>' +
      '                  <span class="dt">' + gettext('Material Category') + '</span>' +
      '                  <span class="dd">' + this['category'] + '</span>' +
      '                </li>' +
      '                <li>' +
      '                  <span class="dt">' + gettext('Abstract') + '</span>' +
      '                  <span class="dd">' + this['abstract'] + '</span>' +
      '                </li>' +
      '                <li>' +
      '                  <span class="dt">DOI</span>' +
      '                  <span class="dd">' + this['doi'] + '</span>' +
      '                </li>' +
      '                  <li>' +
      '                    <span class="dt">' + gettext('Purpose') + '</span>' +
      '                    <span class="dd">' + this['purpose'] + '</span>' +
      '                  </li>' +
      '              </ul>' +
      '            </div>' +
      '            <div class="col-lg-4 col-md-4 col-sm-4">' +
      '             <ul class="list-unstyled">' +
      '                <li>' +
      '                  <span class="dt">' + gettext('Source') + '</span>' +
      '                  <span class="dd">' + this['source.source'] + '</span>' +
      '                </li>' +
      '                <li>' +
      '                  <span class="dt">' + gettext('Reference') + '</span>' +
      '                  <span class="dd">' + this['source.reference'] + '</span>' +
      '                </li>' +
      '                  <li>' +
      '                    <span class="dt">' + gettext('Others') + '</span>' +
      '                    <span class="dd">' + this['source.others'] + '</span>' +
      '                  </li>' +
      '              </ul>' +
      '            </div>' +
      '            <div class="col-lg-4 col-md-4 col-sm-4">' +
      '              <div class="pull-right">' +
      '                <div class="statistic tiny">' +
      '                  <div class="value">' + this['views'] + '</div>' +
      '                  <div class="label">' + gettext('Views') + '</div>' +
      '                </div>' +
      '                <div class="statistic tiny">' +
      '                  <div class="value">' + this['downloads'] + '</div>' +
      '                  <div class="label">' + gettext('Downloads') + '</div>' +
      '                </div>' +
      '                <div class="statistic tiny">' +
      '                  <div class="value">' + this['score'] + '</div>' +
      '                  <div class="label">' + gettext('Score') + '</div>' +
      '                </div>' +
      '              </div>' +
      '            </div>' +
      '          </div>' +
      '        </div>' +
      '     </div>';
    $('#dm-scope').append(obj);
  });
  $('button.btn-download').click(function (event) {
    event.stopPropagation();
    //alert('sdfsgdfg');
    //return true;
    return true;
  })
  // $(".tr-body").on('click', function () {
  //   var dataset_id = $(this).attr("id");
  //   window.open(Urls.resolve('management:show_data', {dataset_id: dataset_id, query_type: 1}));
  // });
}

var author;
var year;
var categroy;

function leftnav_set_hasValue(data, choose_type) {
  if (data.data.total_count === 0) {

  } else {
    $("#nav_categroy").empty();
    $("#nav_year").empty();
    $("#nav_author").empty();
    var content_author = $('#nav_author').html();
    var content_year = $('#nav_year').html();
    var content_domain = $('#nav_categroy').html();
    // alert(data.data.year_num[2015]);
    if (choose_type == null) {
      if (content_author == null || content_author.length == 0) {
        $.each(data.data.author_num, function () {
          var obj1 =
            '<a name="author" href="javascript:void(0)" onclick="get_val(this)" class="OP_LOG_BTN leftnav_tag left_list_short_show" title="' + this['author_id'] + '"<i>' + this['author_id'] + '</i>(' + this['count'] + ')</a>';
          $('#nav_author').append(obj1);
        });
      }
      if (content_year == null || content_year.length == 0) {
        var dic = data.data.year_num;
        var res = Object.keys(dic).sort().reverse();
        for (var key in res) {
          var obj2 =
            '<a name="year" href="javascript:void(0)" onclick="get_val(this)" class="OP_LOG_BTN leftnav_tag left_list_short_show" title="' + res[key] + '" <i>' + res[key] + '以来</i>(' + dic[res[key]] + ')</a>';
          $('#nav_year').append(obj2);
        }
      }
      if (content_domain == null || content_domain.length == 0) {
        $.each(data.data.domain_num, function () {
          var obj3 =
            '<a name="categroy" href="javascript:void(0)" onclick="get_val(this)" class="OP_LOG_BTN leftnav_tag left_list_short_show" data-key="sc_c1" data-index="10" title="' + this['category_id'] + '"<i>' + this['name'] + '</i>(' + this['count'] + ')</a>';
          $('#nav_categroy').append(obj3);
        });
      }
    } else {
      if (choose_type === 0) {
        isauthor = false;
        $.each(data.data.author_num, function () {
          var obj1 =
            '<a name="author" href="javascript:void(0)" onclick="get_val(this)" class="OP_LOG_BTN leftnav_tag left_list_short_show" title="' + this['author_id'] + '"<i>' + this['author_id'] + '</i>(' + this['count'] + ')</a>';
          $('#nav_author').append(obj1);
        });
        if (content_year == "") {
          if (isyear) {
            var dic = data.data.year_num;
            var res = Object.keys(dic).sort().reverse();
            for (var key in res) {
              var obj2 =
                '<a name="year" href="javascript:void(0)" class="OP_LOG_BTN leftnav_tag left_list_short_show" title="' + res[key] + '" <i>' + res[key] + '以来</i><i id="year_c" title="' + res[key] + '" class="c-icon c-icon-close-gray" onclick="cancel_one_search(this)"></a>';
              $('#nav_year').append(obj2);
            }
          } else {
            var dic = data.data.year_num;
            var res = Object.keys(dic).sort().reverse();
            for (var key in res) {
              var obj2 =
                '<a name="year" href="javascript:void(0)" onclick="get_val(this)" class="OP_LOG_BTN leftnav_tag left_list_short_show" title="' + res[key] + '" <i>' + res[key] + '以来</i>(' + dic[res[key]] + ')</a>';
              $('#nav_year').append(obj2);
            }
          }
        }
        if (content_domain == "") {
          if (iscategory) {
            $.each(data.data.domain_num, function () {
              var obj3 =
                '<a name="categroy" href="javascript:void(0)" class="OP_LOG_BTN leftnav_tag left_list_short_show" data-key="sc_c1" data-index="10" title="' + this['category_id'] + '"<i>' + this['name'] + '</i><i id="categroy_c" title="' + this['category_id'] + '" class="c-icon c-icon-close-gray" onclick="cancel_one_search(this)"></i></a>';
              $('#nav_categroy').append(obj3);
            });
          } else {
            $.each(data.data.domain_num, function () {
              var obj3 =
                '<a name="categroy" href="javascript:void(0)" onclick="get_val(this)" class="OP_LOG_BTN leftnav_tag left_list_short_show" data-key="sc_c1" data-index="10" title="' + this['category_id'] + '"<i>' + this['name'] + '</i>(' + this['count'] + ')</a>';
              $('#nav_categroy').append(obj3);
            });
          }
        }
      }
      if (choose_type === 1) {
        isyear = false;
        var dic = data.data.year_num;
        var res = Object.keys(dic).sort().reverse();
        for (var key in res) {
          var obj2 =
            '<a name="year" href="javascript:void(0)" onclick="get_val(this)" class="OP_LOG_BTN leftnav_tag left_list_short_show" title="' + res[key] + '" <i>' + res[key] + '以来</i>(' + dic[res[key]] + ')</a>';
          $('#nav_year').append(obj2);
        }
        if (content_author == "") {
          if (isauthor) {
            $.each(data.data.author_num, function () {
              var obj1 =
                '<a name="author" href="javascript:void(0)" class="OP_LOG_BTN leftnav_tag left_list_short_show" title="' + this['author_id'] + '"<i>' + this['author_id'] + '</i><i id="author_c" title="' + this['author_id'] + '" class="c-icon c-icon-close-gray" onclick="cancel_one_search(this)"></i></a>';
              $('#nav_author').append(obj1);
            });
          } else {
            $.each(data.data.author_num, function () {
              var obj1 =
                '<a name="author" href="javascript:void(0)" onclick="get_val(this)" class="OP_LOG_BTN leftnav_tag left_list_short_show" title="' + this['author_id'] + '"<i>' + this['author_id'] + '</i>(' + this['count'] + ')</a>';
              $('#nav_author').append(obj1);
            });
          }
        }
        if (content_domain == "") {
          if (iscategory) {
            $.each(data.data.domain_num, function () {
              var obj3 =
                '<a name="categroy" href="javascript:void(0)" class="OP_LOG_BTN leftnav_tag left_list_short_show" data-key="sc_c1" data-index="10" title="' + this['category_id'] + '"<i>' + this['name'] + '</i><i id="categroy_c" title="' + this['category_id'] + '" class="c-icon c-icon-close-gray" onclick="cancel_one_search(this)"></i></a>';
              $('#nav_categroy').append(obj3);
            });
          } else {
            $.each(data.data.domain_num, function () {
              var obj3 =
                '<a name="categroy" href="javascript:void(0)" onclick="get_val(this)" class="OP_LOG_BTN leftnav_tag left_list_short_show" data-key="sc_c1" data-index="10" title="' + this['category_id'] + '"<i>' + this['name'] + '</i>(' + this['count'] + ')</a>';
              $('#nav_categroy').append(obj3);
            });
          }
        }
      }
      if (choose_type === 2) {
        iscategory = false;
        $.each(data.data.domain_num, function () {
          var obj3 =
            '<a name="categroy" href="javascript:void(0)" onclick="get_val(this)" class="OP_LOG_BTN leftnav_tag left_list_short_show" data-key="sc_c1" data-index="10" title="' + this['category_id'] + '"<i>' + this['name'] + '</i>(' + this['count'] + ')</a>';
          $('#nav_categroy').append(obj3);
        });
        if (content_author == "") {
          if (isauthor) {
            $.each(data.data.author_num, function () {
              var obj1 =
                '<a name="author" href="javascript:void(0)" class="OP_LOG_BTN leftnav_tag left_list_short_show" title="' + this['author_id'] + '"<i>' + this['author_id'] + '</i><i id="author_c" title="' + this['author_id'] + '" class="c-icon c-icon-close-gray" onclick="cancel_one_search(this)"></i></a>';
              $('#nav_author').append(obj1);
            });
          } else {
            $.each(data.data.author_num, function () {
              var obj1 =
                '<a name="author" href="javascript:void(0)" onclick="get_val(this)" class="OP_LOG_BTN leftnav_tag left_list_short_show" title="' + this['author_id'] + '"<i>' + this['author_id'] + '</i>(' + this['count'] + ')</a>';
              $('#nav_author').append(obj1);
            });
          }
        }
        if (content_year == "") {
          if (isyear) {
            var dic = data.data.year_num;
            var res = Object.keys(dic).sort().reverse();
            for (var key in res) {
              var obj2 =
                '<a name="year" href="javascript:void(0)" class="OP_LOG_BTN leftnav_tag left_list_short_show" title="' + res[key] + '" <i>' + res[key] + '以来</i><i id="year_c" title="' + res[key] + '" class="c-icon c-icon-close-gray" onclick="cancel_one_search(this)"></a>';
              $('#nav_year').append(obj2);
            }
          } else {
            var dic = data.data.year_num;
            var res = Object.keys(dic).sort().reverse();
            for (var key in res) {
              var obj2 =
                '<a name="year" href="javascript:void(0)" onclick="get_val(this)" class="OP_LOG_BTN leftnav_tag left_list_short_show" title="' + res[key] + '" <i>' + res[key] + '以来</i>(' + dic[res[key]] + ')</a>';
              $('#nav_year').append(obj2);
            }
          }
        }
      }
    }
  }
}

function get_val(that) {
  var temp;
  if (that.name === 'author') {
    author = that.title;
    temp = 0;
  }
  if (that.name === 'year') {
    year = that.title;
    temp = 1;
  }
  if (that.name === 'categroy') {
    categroy = Number(that.title);
    temp = 2;
  }
  leftnav_search(1, true, author, year, categroy, temp);
  // search_show_all(1,true,author,year,categroy);
}

function leftnav_search(query_type, time_sort, author, year, categroy, temp) {
  acquire_result(function (data) {
    result_count(data);
    // console.log("callback");
    // if (data.data.total_count === 0) {
    //   search_set_table_noValue(data);
    // } else {
    $('#paginator').empty();
    search_set_table_hasValue(data);
    leftnav_one_set_hasValue(data, temp, author, year, categroy);
    var paginator = $('#paginator');
    var options = {
      bootstrapMajorVersion: 3,
      currentPage: 1,
      numberOfPages: 5,
      totalPages: data.data.page_count,
      onPageClicked: function (e, originalEvent, type, page) {
        acquire_result(search_set_table_hasValue, query_type, time_sort, page, author, year, categroy);
      }
    };
    if (options.totalPages > 1) {
      paginator.bootstrapPaginator(options);
    }
    // }
  }, query_type, time_sort, page = 1, author, year, categroy, temp)
}

function leftnav_one_set_hasValue(data, left_type, author, year, categroy) {
  $("#nav_author").empty();
  $("#nav_year").empty();
  $("#nav_categroy").empty();
  var content_author = $('#nav_author').html();
  var content_year = $('#nav_year').html();
  var content_domain = $('#nav_categroy').html();
  // alert(data.data.year_num[2015]);
  if (left_type === 0) {
    isauthor = true;
    $.each(data.data.author_num, function () {
      var obj1 =
        '<a name="author" href="javascript:void(0)" class="OP_LOG_BTN leftnav_tag left_list_short_show" title="' + this['author_id'] + '"<i>' + this['author_id'] + '</i><i id="author_c" title="' + this['author_id'] + '" class="c-icon c-icon-close-gray" onclick="cancel_one_search(this)"></i></a>';
      $('#nav_author').append(obj1);
    });
    if (content_year == null || content_year.length == 0) {
      if (year == null) {
        var dic = data.data.year_num;
        var res = Object.keys(dic).sort().reverse();
        for (var key in res) {
          var obj2 =
            '<a name="year" href="javascript:void(0)" onclick="get_val(this)" class="OP_LOG_BTN leftnav_tag left_list_short_show" title="' + res[key] + '" <i>' + res[key] + '以来</i>(' + dic[res[key]] + ')</a>';
          $('#nav_year').append(obj2);
        }
      } else {
        var dic = data.data.year_num;
        var res = Object.keys(dic).sort().reverse();
        for (var key in res) {
          var obj2 =
            '<a name="year" href="javascript:void(0)" class="OP_LOG_BTN leftnav_tag left_list_short_show" title="' + res[key] + '" <i>' + res[key] + '以来</i><i id="year_c" title="' + res[key] + '" class="c-icon c-icon-close-gray" onclick="cancel_one_search(this)"></a>';
          $('#nav_year').append(obj2);
        }
      }
    }
    if (content_domain == null || content_domain.length == 0) {
      if (categroy == null) {
        $.each(data.data.domain_num, function () {
          var obj3 =
            '<a name="categroy" href="javascript:void(0)" onclick="get_val(this)" class="OP_LOG_BTN leftnav_tag left_list_short_show" data-key="sc_c1" data-index="10" title="' + this['category_id'] + '"<i>' + this['name'] + '</i>(' + this['count'] + ')</a>';
          $('#nav_categroy').append(obj3);
        });
      } else {
        $.each(data.data.domain_num, function () {
          var obj3 =
            '<a name="categroy" href="javascript:void(0)" class="OP_LOG_BTN leftnav_tag left_list_short_show" data-key="sc_c1" data-index="10" title="' + this['category_id'] + '"<i>' + this['name'] + '</i><i id="categroy_c" title="' + this['category_id'] + '" class="c-icon c-icon-close-gray" onclick="cancel_one_search(this)"></i></a>';
          $('#nav_categroy').append(obj3);
        });
      }
    }
  }
// <<<<<<< HEAD
//     if(content_year == null || content_year.length == 0) {
//         console.log('data');
//         console.log(data);
//         if(data.data.total_count === 0){
//
//         }
//         else{
//             var dic = data.data.year_num;
//             var res = Object.keys(dic).sort().reverse();
//             for(var key in res){
//                 var obj2 =
//                     '<a name="year" href="javascript:void(0)" onclick="get_val(this)" class="OP_LOG_BTN leftnav_tag left_list_short_show" title="'+res[key]+'" <i>' + res[key] + '以来</i>(' + dic[res[key]] + ')</a>';
//                 $('#nav_year').append(obj2);
//                 }
// =======
  if (left_type === 1) {
    isyear = true;
    var dic = data.data.year_num;
    var res = Object.keys(dic).sort().reverse();
    for (var key in res) {
      var obj2 =
        '<a name="year" href="javascript:void(0)" class="OP_LOG_BTN leftnav_tag left_list_short_show" title="' + res[key] + '" <i>' + res[key] + '以来</i><i id="year_c" title="' + res[key] + '" class="c-icon c-icon-close-gray" onclick="cancel_one_search(this)"></a>';
      $('#nav_year').append(obj2);
    }
    if (content_author == null || content_author.length == 0) {
      if (author == null) {
        $.each(data.data.author_num, function () {
          var obj1 =
            '<a name="author" href="javascript:void(0)" onclick="get_val(this)" class="OP_LOG_BTN leftnav_tag left_list_short_show" title="' + this['author_id'] + '"<i>' + this['author_id'] + '</i>(' + this['count'] + ')</a>';
          $('#nav_author').append(obj1);
        });
      } else {
        $.each(data.data.author_num, function () {
          var obj1 =
            '<a name="author" href="javascript:void(0)" class="OP_LOG_BTN leftnav_tag left_list_short_show" title="' + this['author_id'] + '"<i>' + this['author_id'] + '</i><i id="author_c" title="' + this['author_id'] + '" class="c-icon c-icon-close-gray" onclick="cancel_one_search(this)"></i></a>';
          $('#nav_author').append(obj1);
        });
      }
    }
    if (content_domain == null || content_domain.length == 0) {
      if (categroy == null) {
        $.each(data.data.domain_num, function () {
          var obj3 =
            '<a name="categroy" href="javascript:void(0)" onclick="get_val(this)" class="OP_LOG_BTN leftnav_tag left_list_short_show" data-key="sc_c1" data-index="10" title="' + this['category_id'] + '"<i>' + this['name'] + '</i>(' + this['count'] + ')</a>';
          $('#nav_categroy').append(obj3);
        });
      } else {
        $.each(data.data.domain_num, function () {
          var obj3 =
            '<a name="categroy" href="javascript:void(0)" class="OP_LOG_BTN leftnav_tag left_list_short_show" data-key="sc_c1" data-index="10" title="' + this['category_id'] + '"<i>' + this['name'] + '</i><i id="categroy_c" title="' + this['category_id'] + '" class="c-icon c-icon-close-gray" onclick="cancel_one_search(this)"></i></a>';
          $('#nav_categroy').append(obj3);
        });
      }
    }
  }
  if (left_type === 2) {
    iscategory = true;
    $.each(data.data.domain_num, function () {
      var obj3 =
        '<a name="categroy" href="javascript:void(0)" class="OP_LOG_BTN leftnav_tag left_list_short_show" data-key="sc_c1" data-index="10" title="' + this['category_id'] + '"<i>' + this['name'] + '</i><i id="categroy_c" title="' + this['category_id'] + '" class="c-icon c-icon-close-gray" onclick="cancel_one_search(this)"></i></a>';
      $('#nav_categroy').append(obj3);
    });
    if (content_author == null || content_author.length == 0) {
      if (author == null) {
        $.each(data.data.author_num, function () {
          var obj1 =
            '<a name="author" href="javascript:void(0)" onclick="get_val(this)" class="OP_LOG_BTN leftnav_tag left_list_short_show" title="' + this['author_id'] + '"<i>' + this['author_id'] + '</i>(' + this['count'] + ')</a>';
          $('#nav_author').append(obj1);
        });
      } else {
        $.each(data.data.author_num, function () {
          var obj1 =
            '<a name="author" href="javascript:void(0)" class="OP_LOG_BTN leftnav_tag left_list_short_show" title="' + this['author_id'] + '"<i>' + this['author_id'] + '</i><i id="author_c" title="' + this['author_id'] + '" class="c-icon c-icon-close-gray" onclick="cancel_one_search(this)"></i></a>';
          $('#nav_author').append(obj1);
        });
      }
    }
    if (content_year == null || content_year.length == 0) {
      if (year == null) {
        var dic = data.data.year_num;
        var res = Object.keys(dic).sort().reverse();
        for (var key in res) {
          var obj2 =
            '<a name="year" href="javascript:void(0)" onclick="get_val(this)" class="OP_LOG_BTN leftnav_tag left_list_short_show" title="' + res[key] + '" <i>' + res[key] + '以来</i>(' + dic[res[key]] + ')</a>';
          $('#nav_year').append(obj2);
        }
      } else {
        var dic = data.data.year_num;
        var res = Object.keys(dic).sort().reverse();
        for (var key in res) {
          var obj2 =
            '<a name="year" href="javascript:void(0)" class="OP_LOG_BTN leftnav_tag left_list_short_show" title="' + res[key] + '" <i>' + res[key] + '以来</i><i id="year_c" title="' + res[key] + '" class="c-icon c-icon-close-gray" onclick="cancel_one_search(this)"></a>';
          $('#nav_year').append(obj2);
        }
      }
    }
  }
}

var isauthor, isyear, iscategory = false;

function cancel_one_search(that) {
  var choose_type;
  if (that.id === 'author_c') {
    author = null;
    choose_type = 0;
  }
  if (that.id === 'year_c') {
    year = null;
    choose_type = 1;
  }
  if (that.id === 'categroy_c') {
    categroy = null;
    choose_type = 2;
  }
  search_show_all(1, true, author, year, categroy, choose_type);
}

function search_show_all(query_type, time_sort, author, year, categroy, choose_type) {
  acquire_result(function (data) {
    result_count(data);
    // console.log("callback");
    // if (data.data.total_count === 0) {
    //   search_set_table_noValue(data);
    // } else {
    $('#paginator').empty();
    search_set_table_hasValue(data);
    leftnav_set_hasValue(data, choose_type, author, year, categroy);
    var paginator = $('#paginator');
    var options = {
      bootstrapMajorVersion: 3,
      currentPage: 1,
      numberOfPages: 5,
      totalPages: data.data.page_count,
      onPageClicked: function (e, originalEvent, type, page) {
        acquire_result(search_set_table_hasValue, query_type, time_sort, page, author, year, categroy);
      }
    };
    if (options.totalPages > 1) {
      paginator.bootstrapPaginator(options);
    }
    // }
  }, query_type, time_sort, page = 1, author, year, categroy, choose_type)
}

//左侧栏翻转控制
function Hide(collapse, hidediv) {
  $(collapse).on('show.bs.collapse', function () {
    $(hidediv).removeClass('add_transform_down').addClass('add_transform_up');
  });
  $(collapse).on('hide.bs.collapse', function () {
    $(hidediv).removeClass('add_transform_up').addClass('add_transform_down');
  });
}

//fsch 2019-2-28
function getDataMetaPanel(dm) {
  // dm.quantity = dm.quantity || 'N/A';
  dm.abstract = dm.abstract || 'N/A';
  dm.category = dm.category || 'N/A';
  dm.doi = dm.doi || 'N/A';
  dm.purpose = dm.purpose || 'N/A';
  var times = dm.add_time.split('.')[0].split('T');
  var add_time = times[0] + ' ' + times[1];
  return '<div class="panel panel-default">' +
    '  <div class="panel-heading">' +
    '    <a target="_blank" href="' + Urls.resolve('storage:show_data', {did: dm.id}) + '" title="' + gettext('View') + '">' + dm.title + '</a>' +
    '    <div class="pull-right">' +
    '      <a target="_blank" href="' + Urls.resolve('storage:show_data', {did: dm.id}) + '" title="' + gettext('View') + '"><i class="fa fa-external-link"></i></a>' +
    '    </div>' +
    '  </div>' +
    '  <div class="panel-body">' +
    '    <div class="row">' +
    '      <div class="col-sm-12">' +
    '        <p><span class="dt">' + gettext('Add Time') + '</span><span class="dd">' + add_time + '</span></p>' +
    '      </div>' +
    '    </div>' +
    '    <div class="row">' +
    '      <div class="col-sm-3">' +
    '        <p><span class="dt">' + gettext('Views') + '</span><span class="dd">' + dm.views + '</span></p>' +
    '      </div>' +
    '      <div class="col-sm-3">' +
    '        <p><span class="dt">' + gettext('Downloads') + '</span><span class="dd">' + dm.downloads + '</span></p>' +
    '      </div>' +
    '      <div class="col-sm-3">' +
    '        <p><span class="dt">' + gettext('Score') + '</span><span class="dd">' + dm.score + '</span></p>' +
    '      </div>' +
    '    </div>' +
    '    <div class="row">' +
    '      <div class="col-sm-3">' +
    '        <p><span class="dt">' + gettext('Material Class') + '</span><span class="dd">' + dm.category_name + '</span></p>' +
    '      </div>' +
    '      <div class="col-sm-3">' +
    '        <p><span class="dt">' + gettext('Abstract') + '</span><span class="dd">' + dm.abstract + '</span></p>' +
    '      </div>' +
    '      <div class="col-sm-3">' +
    '        <p><span class="dt">DOI</span><span class="dd">' + dm.doi + '</span></p>' +
    '      </div>' +
    '      <div class="col-sm-3">' +
    '        <p><span class="dt">' + gettext('Purpose') + '</span><span class="dd">' + dm.purpose + '</span></p>' +
    '      </div>' +
    '    </div>' +
    '  </div>' +
    '</div>';
}

//另一种显示样式：

// function getDataMetaPanel(dm){
// return '<div class="data-meta panel" data-id="' + dm.id + '" onclick="dataOnClick(this)">' +
// '        <div class="panel-heading">' +
// '          <span class="d-title">'+ dm.title + '</span>' +
// '              <span class="d-labels">' +
// '                  <span class="label label-primary">' +
// '                    <i class="fa fa-tag"></i>'+ dm.keywords + '' +
// '                  </span>' +
// '              </span>' +
// '<div id="download">'+
//     '          <button class="btn btn-default btn-sm btn-add-to-cart" data-tid="'+dm.tid+'" data-id="'+dm.id+'"' +
//     '                  style="float:right"' +
//     '                  data-title="'+dm.title+'" data-toggle="tooltip" data-placement="left"' +
//     '                  title="' + gettext('Add to download list') + '">' +
//     '            <i class="fa fa-plus"></i>' +
//     '          </button>' +
//     '          <button class="btn btn-default btn-sm dropdown-toggle" id="download_single" type="button" data-toggle="dropdown" style="float: right">'+
//     '             <i class="fa fa-download"></i>'+
//     '             <span class="caret"></span>'+
//     '         </button>'+
//     '         <ul class="dropdown-menu dropdown-menu-right" role="menu">'+
//     '             <li><a>JSON</a></li>'+
//     '             <li><a>XML</a></li>'+
//     '             <li><a>EXCEL</a></li>'+
//     '         </ul>'+
// '</div>'+
// '        </div>' +
// '        <div class="panel-body">' +
// '          <p>' +
// '            <span class="dm dt">' + gettext('Author')+'</span>' +
// '            <a href="#" class="dm dd author">'+dm.author +'</a>' +
// '            <span class="dm dt">' + gettext('Add Time')+'</span>' +
// '            <span class="dm dd date">'+ dm.add_time +'</span>' +
// '          </p>' +
// '          <div class="row">' +
// '            <div class="col-lg-4 col-md-4 col-sm-4">' +
// '              <ul class="list-unstyled">' +
// '                <li>' +
// '                  <span class="dt">'+gettext('Material Category')+'</span>' +
// '                  <span class="dd">'+dm.category +'</span>' +
// '                </li>' +
// '                <li>' +
// '                  <span class="dt">' +gettext('Abstract')+'</span>' +
// '                  <span class="dd">'+dm.abstract +'</span>' +
// '                </li>' +
// '                <li>' +
// '                  <span class="dt">DOI</span>' +
// '                  <span class="dd">'+dm.doi +'</span>' +
// '                </li>' +
// '                  <li>' +
// '                    <span class="dt">'+gettext('Purpose' )+'</span>' +
// '                    <span class="dd">'+dm.purpose +'</span>' +
// '                  </li>' +
// '              </ul>' +
// '            </div>' +
// '            <div class="col-lg-4 col-md-4 col-sm-4">' +
// '             <ul class="list-unstyled">' +
// '                <li>' +
// '                  <span class="dt">'+gettext('Source')+'</span>' +
// '                  <span class="dd">'+dm.source.source  +'</span>' +
// '                </li>' +
// '                <li>' +
// '                  <span class="dt">'+gettext('Reference')+'</span>' +
// '                  <span class="dd">'+dm.source.reference +'</span>' +
// '                </li>' +
// '                  <li>' +
// '                    <span class="dt">'+gettext('Others')+'</span>' +
// '                    <span class="dd">'+ dm.source.others  +'</span>' +
// '                  </li>' +
// '              </ul>' +
// '            </div>' +
// '            <div class="col-lg-4 col-md-4 col-sm-4">' +
// '              <div class="pull-right">' +
// '                <div class="statistic tiny">' +
// '                  <div class="value">'+dm.views +'</div>' +
// '                  <div class="label">'+gettext('Views')+'</div>' +
// '                </div>' +
// '                <div class="statistic tiny">' +
// '                  <div class="value">'+ dm.downloads +'</div>' +
// '                  <div class="label">'+gettext('Downloads')+'</div>' +
// '                </div>' +
// '                <div class="statistic tiny">' +
// '                  <div class="value">'+dm.score +'</div>' +
// '                  <div class="label">'+gettext('Score')+'</div>' +
// '                </div>' +
// '              </div>' +
// '            </div>' +
// '          </div>' +
// '        </div>' +
// '     </div>';
// }

function getHot() {
  // page = page || 1;
  // perPage = perPage || PER_PAGE;
  $.get(Urls.resolve('search_api_v1:hot_list')
  ).done(function (resp) {
    // console.log(resp.data);
    // $("#hot-count").html(resp.data.count);
    var hotScope = $('#hot-scope');
    // hotScope.empty();
    if (resp.data.results.length != 0) {
      resp.data.results.forEach(function (it) {
        hotScope.append(getDataMetaPanel(it));
      });
    } else {
      // $("#hot_result").hide();
      hotScope.append('<p>' + gettext('Sorry, there is no data for the latest week!') + '</p>');
    }
    // CURRENT_PAGE = page;
    // TOTAL_PAGE = resp.data.total_pages;
    // if (TOTAL_PAGE > 1) {
    //   var $pScope = $('#ta-pagination-scope');
    //   $pScope.empty();
    //   $pScope.append(getPaginationNav(CURRENT_PAGE, TOTAL_PAGE, 5, function (pageNo) {
    //     getAll(pageNo, perPage);
    //   }));
    // }
  }).fail(function () {
    window._jqHandled = true;
  });
}

function save_record(text) {
  $.requestJSON({
    url: Urls.resolve('storage_api_v1:record'),
    method: 'POST',
    data: {
      text: text
    },
    silentError: true,
    onSuccess: function (data) {
      console.log(data);
    },
    onError: function (data) {
      $.showModal({
        title: gettext('ERROR') + ' ' + data.code,
        message: gettext('An error has occurred! Please check your data and try again.')
      });
    }
  });
}

var type_string;
var text;
var time_sort;

$(function () {
  $('#nav_author').empty();
  $("#nav_year").empty();
  $("#nav_categroy").empty();
  $("#con_result").hide();

  //  fsch 2019-2-28
  getHot();

  $('#com-search-btn').on('click', function () {
    type_string = $('#query-type').val();
    text = $("#com-search-text").val().trim();
    if (text === '') {

    } else {
      search_show_all(1, true, null, null, null, null);
      $("#hot_result").hide();
      $("#con_result").show();
      save_record(text);
    }

    // else {
    //     var q = {field: type_string, value: text};
    //     params = {
    //       type: 'field',
    //       query: $.base64.btoa(JSON.stringify(q), true),
    //     }
    //   }
    // else {
    //   if (type_string === 'full') {
    //     params = {
    //       type: 'full',
    //       query: $.base64.btoa(text, true),
    //       page: 1
    //     }
    //   }
    //   else if (type_string === 'expr') {
    //     params = {
    //       type: 'expr',
    //       query: $.base64.btoa(text, true),
    //       page: 1
    //     }
    //   }
    //   else {
    //     var q = {field: type_string, value: text};
    //     params = {
    //       type: 'field',
    //       query: $.base64.btoa(JSON.stringify(q), true),
    //     }
    //   }
    // }
    // window.location = Urls.resolve('search:search') + '/?' + $.param(params);
  });

  // 绑定回车
  $("#com-search-text").bind('keypress', function (event) {
    if (event.keyCode === 13) {
      $('#com-search-btn').click();
    }
  });
  $('#sort-btn').on('click', function () {
    var sort_order = $('#sort-order').val();
    if (sort_order === "desc") {
      time_sort = true;
      search_show_all(1, time_sort, null, null, null, null);
    } else {
      time_sort = false;
      search_show_all(1, time_sort, null, null, null, null);
    }
  });
  $('#json').click(function () {
    export_data()
  })


  // $("#download_single").on('click', function (e) {
  //
  //     // e.stopPropagation();
  //     export_data();
  // });


  // document.getElementById("#con_result").style.visibility="hidden";//隐藏

  // $("#con_result").style.visibility="hidden" ;


});

